<template>
  <div>
    <!--    面包屑导航-->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/administration' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>新闻管理</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="box-card">
      <!--      搜索添加-->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
                  placeholder="搜索新闻"
                  prefix-icon="el-icon-search"
                  @blur="searchNews"
                  @clear="searchNews"
                  v-model="keyword"
                  clearable>
          </el-input>
        </el-col>
        <el-col :span="2.5">
          <router-link to="/editingNews">
            <el-button type="primary">添加新闻</el-button>
          </router-link>
        </el-col>
        <el-col :span="2.5">
          <el-button type="success" @click="addTopicDialogVisible = true">添加主题</el-button>
        </el-col>
        <el-col :span="2.5">
          <el-button type="danger" @click="openRemoveDialog">删除主题</el-button>
        </el-col>
      </el-row>

      <!--    新闻列表-->
      <el-table border :data="tablePageData" style="width: 100%">
        <el-table-column
                type="index"
                width="50">
        </el-table-column>
        <el-table-column
                prop="title"
                label="标题">
        </el-table-column>
        <!--        <el-table-column-->
        <!--                prop="content"-->
        <!--                label="内容">-->
        <!--        </el-table-column>-->
        <el-table-column
                prop="author"
                label="作者">
        </el-table-column>
        <el-table-column
                prop="topic"
                label="主题">
        </el-table-column>
        <el-table-column
                prop="date"
                label="时间">
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <router-link :to="{path:'/editingNews',query:{id : scope.row._id}}" style="margin-right: 10px">
              <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
            </router-link>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeNews(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!--      分页-->
      <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="currentPage"
              :page-sizes="[3, 5, 10, 20]"
              :page-size="pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="tableData.length">
      </el-pagination>
    </el-card>

    <!--    添加主题-->
    <el-dialog
            title="添加主题"
            :visible.sync="addTopicDialogVisible"
            width="30%"
            @close="resetForm">

      <el-form :model="TopicForm" :rules="topicRules" ref="addTopicRuleForm" label-width="100px">
        <el-form-item label="主题名称" prop="topic">
          <el-input v-model="TopicForm.topic"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="addTopicDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addTopic">确 定</el-button>
      </span>
    </el-dialog>

    <!--    删除主题-->
    <el-dialog
            title="删除主题"
            :visible.sync="removeTopicDialogVisible"
            width="30%"
            @close="resetForm">

      <el-form :model="topicName" :rules="topicRules" ref="removeTopicRuleForm" label-width="100px">
        <el-form-item label="活动名称" prop="topicName">
          <el-select v-model="topicName.topicName" placeholder="请选择">
            <el-option
                    v-for="item in topic"
                    :label="item.topic"
                    :value="item.topic">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="removeTopicDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="removeTopic">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    name: "news",
    data() {
      return {
        tableData: [],
        tablePageData: [],
        keyword: '',
        addTopicDialogVisible: false,
        removeTopicDialogVisible: false,
        TopicForm: {
          topic: ''
        },
        topicRules: {
          topic: [{required: true, message: '请填写主题名称', trigger: 'blur'}],
          topicName: [{required: true, message: '请选择要删除的主题', trigger: 'blur'}],
        },
        topicName: {
          topicName: ''
        },
        topic: [],
        currentPage: 1,
        pageSize: 5
      }
    },
    methods: {
      getList() {
        this.$store.dispatch('getNews')
          .then(() => {
            this.tableData = this.$store.state.news
            this.handleCurrentChange(this.currentPage)
            if (this.tablePageData.length === 0 && this.currentPage > 0) {
              this.currentPage--
              this.handleCurrentChange(this.currentPage)
            }
          })
      },
      removeNews(row) {
        this.$confirm('确定删除这条新闻吗?', '确认删除', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$store.dispatch('removeNews', row._id)
            .then(() => {
              this.$message.success('删除成功')
              this.getList()
            })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      searchNews() {
        this.$store.dispatch('searchNews', {keyword: this.keyword.trim()})
          .then(() => {
            this.tableData = this.$store.state.news
            this.handleCurrentChange(1)
          })
      },
      openRemoveDialog() {
        this.$store.dispatch('getTopic')
          .then(() => {
            this.topic = this.$store.state.topic
            this.removeTopicDialogVisible = true
          })
      },
      addTopic() {
        this.$refs.addTopicRuleForm.validate((valid) => {
          if (valid) {
            this.$store.dispatch('addTopic', this.TopicForm)
              .then(() => {
                this.addTopicDialogVisible = false
                this.getList()
              })
          } else {
            console.log('error submit!!');
            return false;
          }
        })
      },
      removeTopic() {
        this.$refs.removeTopicRuleForm.validate((valid) => {
          if (valid) {
            this.$confirm('确定删除此主题以及相关的所有新闻吗?', '确认删除', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              this.$store.dispatch('removeTopic', this.topicName.topicName)
                .then(() => {
                  this.$message.success('删除成功')
                  this.removeTopicDialogVisible = false
                  this.getList()
                })
            }).catch(() => {
              this.$message({
                type: 'info',
                message: '已取消删除'
              });
            });
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm() {
        if (this.$refs.addTopicRuleForm) {
          this.$refs.addTopicRuleForm.resetFields();
        }
        if (this.$refs.removeTopicRuleForm) {
          this.$refs.removeTopicRuleForm.resetFields();
        }
      },
      handleSizeChange(size) {
        this.pageSize = size
        this.handleCurrentChange(this.currentPage)
      },  //切换条数
      handleCurrentChange(page) {
        this.currentPage = page
        this.tablePageData = this.tableData.filter((item, index) => index >= this.pageSize * (this.currentPage - 1) && index < this.pageSize * this.currentPage)
      },  //切换页数
    },
    created() {
      this.getList()
    }
  }
</script>

<style scoped>
  .el-breadcrumb {
    margin-bottom: 20px;
  }

  .el-table {
    margin-top: 20px;
  }

</style>